package com.xyc.servlet.main;

import com.xyc.mapper.Admin_userMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;

@WebServlet("/checkName")
public class CheckNameServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");

        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-dao.xml");
        Admin_userMapper mapper = ctx.getBean("admin_userMapper", Admin_userMapper.class);
        int flag = mapper.selectCount(username);

        Writer out = resp.getWriter();
        if (flag > 0) {
            out.write("yes");
        } else {
            out.write("no");
        }
    }
}
